package com.sea.base.utils.voice

import android.graphics.drawable.AnimationDrawable
import android.view.View
import androidx.collection.ArrayMap
import androidx.recyclerview.widget.RecyclerView
import com.sea.base.ext.view.postLifecycle
import com.sea.base.ui.IUIContext
import com.sea.base.widget.PlayPauseImageView
import com.sea.base.widget.WaveVoiceAnimView

/**
 * 在Adapter中播放的通用处理
 */
class AdapterVoicePlayUtil(ui: com.sea.base.ui.IUIContext) {
    private val player = VoiceMediaPlayer(ui.lifecycle)

    private val animMap = ArrayMap<String, IAdapterAnimListener>()

    /**
     * 绑定item数据，当[RecyclerView.Adapter.bindViewHolder]时调用保存需要播放的view
     * @param anim [AnimationDrawableListenerImpl]、[ImageAnimListenerImpl]或者自行实现
     */
    fun bindAnim(view: View, url: String, anim: IAdapterAnimListener) {
        animMap[url] = anim
        val currentItem = player.currentItem
        if (currentItem?.urlOrPath == url && currentItem.isPlaying) {
            anim.start()
        } else {
            view.postLifecycle {
                anim.stop()
            }
        }
    }

    /**
     * 切换播放状态，当点击时调用切换成暂停或播放
     */
    fun togglePlay(newUrl: String) {
        if (newUrl.isEmpty()) {
            return
        }
        val currentItem = player.currentItem
        if (currentItem?.urlOrPath == newUrl) {
            if (currentItem.isPlaying) {
                currentItem.pause()
                animMap[newUrl]?.pause()
            } else {
                currentItem.play()
                animMap[newUrl]?.start()
            }
        } else {
            if (currentItem != null) {
                currentItem.stop()
                animMap[currentItem.urlOrPath]?.stop()
            }
            val item = player.play(newUrl)
            animMap[newUrl]?.start()
            item.onPauseListener = {
                animMap[newUrl]?.pause()
            }
            item.onCompletionListener = {
                animMap[newUrl]?.stop()
            }
        }
    }
}

interface IAdapterAnimListener {
    fun start()
    fun pause()
    fun stop()
}

open class AnimationDrawableListenerImpl(
    private val anim: AnimationDrawable,
) : IAdapterAnimListener {
    override fun start() {
        anim.start()
    }

    override fun pause() {
        anim.stop()
    }

    override fun stop() {
        anim.stop()
        anim.selectDrawable(0)
    }
}

class ImageAnimListenerImpl(
    anim: AnimationDrawable,
    private val imageView: PlayPauseImageView,
) : AnimationDrawableListenerImpl(anim) {
    override fun start() {
        super.start()
        imageView.playType = PlayPauseImageView.PLAY_TYPE_PLAYING
    }

    override fun pause() {
        super.pause()
        imageView.playType = PlayPauseImageView.PLAY_TYPE_PAUSE_ING
    }

    override fun stop() {
        super.stop()
        imageView.playType = PlayPauseImageView.PLAY_TYPE_PAUSE_ING
    }
}

class ImageWaveAnimListenerImpl(
    private val anim: WaveVoiceAnimView,
    private val imageView: PlayPauseImageView,
) : IAdapterAnimListener {
    override fun start() {
        anim.start()
        imageView.playType = PlayPauseImageView.PLAY_TYPE_PLAYING
    }

    override fun pause() {
        anim.pause()
        imageView.playType = PlayPauseImageView.PLAY_TYPE_PAUSE_ING
    }

    override fun stop() {
        anim.stop()
        imageView.playType = PlayPauseImageView.PLAY_TYPE_PAUSE_ING
    }
}